package com.haipumi.back.actions;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.haipumi.beans.DataGrid;
import com.haipumi.beans.haipumi_product;
import com.haipumi.biz.BackGoodsBiz;
import com.haipumi.servlets.CommonServlet;
import com.haipumi.utils.CommonData;
import com.haipumi.utils.RequestUtil;
import com.haipumi.utils.YcStringUtils;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class BackGoodsAction extends CommonAction {

	
	@Autowired
	private BackGoodsBiz backGoodsBiz;
	
	DataGrid dg=new DataGrid();
	
	public DataGrid getDg() {
		return dg;
	}

	public String doGoods() throws UnsupportedEncodingException{
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpServletResponse response=ServletActionContext.getResponse();
		String op=request.getParameter("op");
		String pid=request.getParameter("pid");
		if(op!=null){
			if("findGoods".equals(op)){
				findGoodsOp(request,response);
			}else if("addGoods".equals(op)){
				addGoodsOp(request,response);
			}else if("updateGoods".equals(op)){
				updateGoodsOp(request,response);
			}else if("updateGoodsDetail".equals(op)){
				updateGoodsDetailOp(request,response);
			}else if("changeStatus".equals(op)){
				changeStatusOp(request,response);
			}
		}
		return Action.SUCCESS;
	}
	
	
	//上下架操作
	private void changeStatusOp(HttpServletRequest request,
			HttpServletResponse response) {
		haipumi_product goods=RequestUtil.parseRequest(request, haipumi_product.class);
		haipumi_product backgoods=null;
		backgoods=backGoodsBiz.updateGoodsStatus(goods);
		if(backgoods!=null){
			List<haipumi_product> list=(List<haipumi_product>) request.getSession().getServletContext().getAttribute(CommonData.APPLICATIONPRODUCTKEY);
			for(int i=0;i<list.size();i++){
				if(list.get(i).getPid()==goods.getPid()){
					list.get(i).setPstatus(goods.getPstatus());//修改详情
					break;
				}
			}
			request.getSession().getServletContext().setAttribute(CommonData.APPLICATIONPRODUCTKEY, list);
			dg.setCode(1);
			dg.setMsg("状态修改成功");
		}else{
			dg.setCode(0);
			dg.setMsg("状态修改失败");
		}
	}

	//更新奖品描述
	private void updateGoodsDetailOp(HttpServletRequest request,
			HttpServletResponse response) {
		haipumi_product goods=RequestUtil.parseRequest(request, haipumi_product.class);
		//对详情信息进行js过滤
		goods.setPdescribe( YcStringUtils.javascriptEscape(goods.getPdescribe())  );
		haipumi_product backgoods=null;
		backgoods =backGoodsBiz.updateGoodsDetail(goods);
		if(backgoods!=null){
			List<haipumi_product> list=(List<haipumi_product>) request.getSession().getServletContext().getAttribute(CommonData.APPLICATIONPRODUCTKEY);
			for(int i=0;i<list.size();i++){
				if(list.get(i).getPid()==goods.getPid()){
					list.get(i).setPdescribe(goods.getPdescribe());//修改详情
					break;
				}
			}
			request.getSession().getServletContext().setAttribute(CommonData.APPLICATIONPRODUCTKEY, list);
			dg.setCode(1);
			dg.setMsg("修改成功");
		}else{
			dg.setCode(0);
			dg.setMsg("修改失败");
		}
	}

	private void updateGoodsOp(HttpServletRequest request,
			HttpServletResponse response) {
		haipumi_product goods=RequestUtil.parseRequest(request, haipumi_product.class);
		try {
			haipumi_product backgoods=null;
			backgoods=backGoodsBiz.updateGoods(goods);
			if(backgoods!=null){
				List<haipumi_product> list=(List<haipumi_product>) request.getSession().getServletContext().getAttribute(CommonData.APPLICATIONPRODUCTKEY);
				for(int i=0;i<list.size();i++){
					if(list.get(i).getPid()==goods.getPid()){
						list.set(i, goods);
						break;
					}
				}
				request.getSession().getServletContext().setAttribute(CommonData.APPLICATIONPRODUCTKEY, list);
				dg.setCode(1);
				dg.setMsg("修改成功");
			}else{
				dg.setCode(0);
				dg.setMsg("修改失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			dg.setCode(0);
			dg.setMsg("未知原因修改失败，请联系管理员");
		}
	}

	private void addGoodsOp(HttpServletRequest request, HttpServletResponse response) {
		haipumi_product goods=RequestUtil.parseRequest(request, haipumi_product.class);
//		try {
//			//添加goods,并且修改application
//			goods=gb.addGoods(goods);
//			List<haipumi_product> list=(List<haipumi_product>) request.getSession().getServletContext().getAttribute(CommonData.APPLICATIONPRODUCTKEY);
//			list.add(goods);
//			request.getSession().getServletContext().setAttribute(CommonData.APPLICATIONPRODUCTKEY, list);
//			super.outData(response, goods);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		dg.setRows(backGoodsBiz.addGoods(goods));
		dg.setTotal(backGoodsBiz.findGoodsCount(start,end,sort,order,null));
	}

	private void findGoodsOp(HttpServletRequest request,
			HttpServletResponse response) throws UnsupportedEncodingException {
//		String pname="";
//		if(request.getParameter("pname")!=null && !"".equals(request.getParameter("pname")) ){
//			pname=request.getParameter("pname");
//		}
//		int total=gb.findGoodsCount(pname);
//		List<haipumi_product> list=gb.findGoods(pname,rows, page, sort, order);
//		//因为easyui的datagrid 控件要求回传的数据格式必须是这样的 rows:[] ,total:xx
//		
//		dgm.setRows(list);
//		dgm.setTotal(total);
//		super.outData(response, dgm);
		
		dg.setRows(backGoodsBiz.findAllGoods(start,end,sort,order));
		dg.setTotal(backGoodsBiz.findGoodsCount(start,end,sort,order,null));
	}

}
